package cn.b.sky.common.utils;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;

import java.util.Map;

/**
 * 
 * <strong>Title : PropertiesDataBinder.java<br></strong>
 * <strong>Package : cn.b.sky.common.utils<br></strong>
 * <strong>Description : </strong>属性注入，调用springmvc内部机制<br> 
 * <strong>Create on : 2015-12-8 下午3:47:42<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class PropertiesDataBinder extends WebDataBinder {

  public PropertiesDataBinder(Object target) {
    super(target);
  }

  public PropertiesDataBinder(Object target, String objectName) {
    super(target, objectName);
  }
  
  public Object getTarget(Map<String, Object> map) {
    if (map != null && !map.isEmpty()) {
      MutablePropertyValues mpvs = new MutablePropertyValues(map);
      doBind(mpvs);
    }
    return super.getTarget();
  }

  @Override
  public void doBind(MutablePropertyValues mpvs) {
    super.doBind(mpvs);
  }

  public static PropertiesDataBinder createBinder(Object target) {
    ConfigurableWebBindingInitializer bindingInitializer = SpringBeanUtil
        .getBean(ConfigurableWebBindingInitializer.class);
    PropertiesDataBinder binder = new PropertiesDataBinder(target);
    bindingInitializer.initBinder(binder, null);
    return binder;
  }
}
